Option Strict Off
Option Explicit On
Imports SlimDX
Imports SlimDX.Direct3D9
Imports System.Terrain

Public Class MeshBuilder
    Enum EnumMesh
        Primitive
        Mesh
        Teapot
    End Enum
    Private verts() As Vector3 ' 28  
    Private TCTVerts() As TransformedColoredTexturedVertex
    Public vCount As Integer
    Dim indices() As Short ' 36
    Public iCount As Integer
    Dim v2D() As Vector2
    Dim nv As Short
    Public MeshType As EnumMesh = EnumMesh.Mesh
    Dim FaceCount As Integer

    Const BG As Short = 32000
    Dim Bx As Box



    Public TextureMode As Short ' 0=1:1, 1-By TextelW,TexelV, 2-EnvironmentMapping

    ' Means
    ' 0=1:1 the whole texture will be applyed by a single face of the mesh. an image will be projected on a single object face
    ' 1-By TextelW,TexelV  The theture will be applyed and repeat U times per horizontal face and V times per vertical face
    ' 2-EnvironmentMapping: a single image will be placed on all object indipendtly of its faces number
    '


    Public TexelU As Single
    Public TexelV As Single

    Public Sub Dispose()

    End Sub


    Private Function GetTexCoord(P As Vector2) As Vector2
        Dim uv As Vector2
        Try
            If P.X <> 0 Then uv.X = (1 / ((Bx.Max.X - Bx.Min.X) / ((Bx.Min.X) + P.X)))
            If P.Y <> 0 Then uv.Y = (1 / ((Bx.Max.Y - Bx.Min.Y) / ((Bx.Min.Y) + P.Y)))
        Catch ex As Exception

        End Try

        Return uv
    End Function

    Function Line(x1 As Single, y1 As Single, z1 As Single, x2 As Single, y2 As Single, z2 As Single) As Direct3D9.Mesh
        MeshType = EnumMesh.Primitive

        AddVertex(x1, y1, z1, 0, 0, 0)
        AddVertex(x2, y2, z2, 0, 0, 0)

        iCount = 2
        ReDim Preserve indices(iCount - 1)

        indices(0) = 0
        indices(1) = 1

        Line = ResolveMesh()

    End Function

    Sub LoadDXF(file As String)

        Dim Vert(3) As Vector3
        Dim Col As Short
        Dim v() As Vector3
        Dim n As Integer
        Dim i2, i0, i1, i3 As Short
        Dim k, i, iv, s, nPoly As Short
        Dim riga As String
        Dim FINP As Short
        Dim Stato As Short
        Dim dxfgroup As Short
        Dim dxfValue As String
        Dim ok As Boolean
        Dim nometex As String
        Dim P As Vector3
        Dim uv As Vector3
        Dim ue As Single


        ' Dim Bx As Box3D
        n = -1



        On Error GoTo rdxf_err

        FINP = FreeFile()


        FileOpen(FINP, file, OpenMode.Input)
        Stato = 0

        Do While Not (EOF(FINP))
            riga = LineInput(FINP)
            dxfgroup = Val(riga)
            riga = LineInput(FINP)
            dxfValue = UCase(riga)
            Select Case Stato
                Case 0
                    If dxfgroup = 0 And dxfValue = "SECTION" Then Stato = 1
                Case 1
                    If dxfgroup = 2 And dxfValue = "ENTITIES" Then
                        Stato = 2
                    Else
                        Stato = 0
                    End If
                Case 2
                    If dxfgroup = 0 And dxfValue = "3DFACE" Then
                        ok = True
                        iv = 0
                        Stato = 3
                    End If

                Case 3
                    Select Case dxfgroup
                        Case 0 '---- nuova stringa
                            If ok Then
                                n = n + 4
                                ReDim Preserve v(n)
                                For i = 0 To 3
                                    v(n - (3 - i)) = Vert(i)
                                    Bx.Max.X = Max(Vert(i).X, Bx.Max.X)
                                    Bx.Max.Y = Max(Vert(i).Y, Bx.Max.Y)
                                    Bx.Min.X = Min(Vert(i).X, Bx.Min.X)
                                    Bx.Min.Y = Min(Vert(i).Y, Bx.Min.Y)
                                    Bx.Max.Z = Max(Vert(i).Z, Bx.Max.Z)
                                    Bx.Min.Z = Min(Vert(i).Z, Bx.Min.Z)
                                Next i
                            End If
                            ok = dxfValue = "3DFACE"
                            iv = 0
                            nometex = ""
                            Col = 0
                            If dxfValue = "ENDSEC" Or dxfValue = "EOF" Then
                                Stato = 99
                            End If
                        Case 10 To 19 '---- coordinata X
                            If dxfgroup = 11 Or dxfgroup = 12 Or dxfgroup = 13 Then
                                iv = iv + 1
                            End If
                            Vert(iv).X = Val(dxfValue)
                        Case 20 To 29 '---- coordinata Y
                            Vert(iv).Y = Val(dxfValue)
                        Case 30 To 39 '---- coordinata Z
                            Vert(iv).Z = Val(dxfValue)
                        Case 62 '---- colore
                            Col = Val(dxfValue)
                        Case 999 '---- comment for texture; format "999 T nametex"
                            If Left(dxfValue, 1) = "T" Then
                                nometex = Mid(dxfValue, 3)
                            End If
                    End Select
                Case 99
                    Exit Do
            End Select
        Loop
        FileClose(FINP)

        k = 0
        For i = 0 To n
            P = v(i)
            uv.X = 1 / ((Bx.Max.X - Bx.Min.X) / ((Bx.Min.X) + P.X))
            uv.Y = 1 / ((Bx.Max.Y - Bx.Min.Y) / ((Bx.Min.Y) + P.Y))
            uv.Z = 1 / ((Bx.Max.Z - Bx.Min.Z) / ((Bx.Min.Z) + P.Z))

            If uv.Y > uv.Z Then ue = uv.Z Else ue = uv.Y

            AddVertex(P.X, P.Y, P.Z, 0, uv.X, uv.Y)
            k = k + 1
            If k = 4 Then
                i0 = vCount - 4 : i1 = vCount - 3 : i2 = vCount - 2 : i3 = vCount - 1
                ConnectIndexes(i0, i1, i2)
                ConnectIndexes(i0, i2, i3)
                k = 0
            End If
        Next i

        Exit Sub

rdxf_err:

        MsgBox(Err.Description, 16, "Error in loading DXF file")
        Resume Next
  

    End Sub



    Sub AddProfileVertex(x As Single, y As Single)
        nv = nv + 1
        ReDim Preserve v2D(nv)
        v2D(nv).X = x
        v2D(nv).Y = y

        Bx.Max.X = Max(x, Bx.Max.X)
        Bx.Max.Y = Max(y, Bx.Max.Y)
        Bx.Min.X = Min(x, Bx.Min.X)
        Bx.Min.Y = Min(y, Bx.Min.Y)

    End Sub


    Sub AddVertex(x As Single, y As Single, z As Single, w As Single, tu As Single, tv As Single)
        Dim v As New TransformedColoredTexturedVertex(New Vector4(x, y, z, w), QBColor(4), New Vector2(tu, tv))
        vCount += 1
        ReDim Preserve TCTVerts(vCount - 1)
        TCTVerts(vCount - 1) = v
    End Sub

    Sub ConnectIndexes(i1 As Short, i2 As Short, i3 As Short)

        ' Connect indexes by triangles
        If i1 < 0 Then
            MsgBox("ConnectIndexes error")
            Return
        End If

        iCount = iCount + 3
        ReDim Preserve indices(iCount - 1)

        indices(iCount - 3) = i1
        indices(iCount - 2) = i2
        indices(iCount - 1) = i3

    End Sub

    Sub Extrude2(depth As Single)

        ' this went my meshbuilder

        'Dim Tr As New Triangulator
        'Dim pt As New PointList
        'Dim Tri As New TriangleMesh


        'Dim v, t(2) As Vector3
        'Dim i2, i, i0, i1, i3, J As Short
        'Dim h, b As Single

        'Dim P(2) As Vector2

        'Dim h0 As Boolean

        'b = 0
        'h = depth
        'h0 = h = 0 ' Depthno passed, assumes to get a simple polygon
        'FaceCount = 0
        'If 1 = 1 Then

        '    For i = 1 To nv
        '        pt.AddVertex(v2D(i).X, v2D(i).Y, 0)
        '    Next
        '    Tr.Initialise(pt, Nothing)

        '    Tri = Tr.GetTriangleMesh
        '    Tri = Tri.Extrude(1, depth, Nothing)
        '    ''  Debug.Print("---- VERTS")
        '    'For i = 1 To Tri.PointCount

        '    '    Tri.GetPoint(i, v.X, v.Y, v.Z)
        '    '    Debug.Print(i - 1 & ")" & v.ToString)

        '    '    '  AddVertex(v.X, v.Y, v.Z, 1, 0, 0)
        '    'Next
        '    'Debug.Print("---- VERTS")

        '    For i = 1 To Tri.TriangleCount
        '        Tri.GetTriangleIndexes(i, i1, i2, i3)

        '        Tri.TextureCoord(i, P(0).X, P(0).Y, P(1).X, P(1).Y, P(2).X, P(2).Y)
        '        Tri.GetTriangle(i, t(0).X, t(0).Y, t(0).Z, t(1).X, t(1).Y, t(1).Z, t(2).X, t(2).Y, t(2).Z)
        '        For J = 0 To 2
        '            AddVertex(t(J).X, t(J).Y, t(J).Z, 1, P(J).X, P(J).Y)
        '        Next

        '        'TCTVerts(i1 - 1).TextureCoordinates = P(0)
        '        'TCTVerts(i2 - 1).TextureCoordinates = P(1)
        '        'TCTVerts(i3 - 1).TextureCoordinates = P(2)

        '        'ConnectIndexes(i1 - 1, i2 - 1, i3 - 1)
        '        'FaceCount += 1
        '        'Debug.Print(FaceCount & ")" & i1 - 1 & "," & i2 - 1 & "," & i3 - 1)
        '        i0 = vCount - 3 : i1 = vCount - 2 : i2 = vCount - 1
        '        ConnectIndexes(i0, i1, i2)
        '        'FaceCount += 1
        '    Next
        'Else
        '    'For i = 1 To nv
        '    '    AddVertex(v2D(i).X, v2D(i).Y, 0, 1, 0, 0)
        '    '    ' pt.AddVertex(v2D(i).X, v2D(i).Y, 0)
        '    'Next
        '    'For i = 1 To nv
        '    '    AddVertex(v2D(i).X, v2D(i).Y, h, 1, 0, 0)
        '    '    ' pt.AddVertex(v2D(i).X, v2D(i).Y, 0)
        '    'Next
        'End If

        ''ConnectIndexes(0, 1, 2)
        ''ConnectIndexes(0, 1, 3)

        ''ConnectIndexes(0, 4, 3)
        ''ConnectIndexes(3, 4, 7)

        ''ConnectIndexes(0, 4, 6)
        ''ConnectIndexes(6, 5, 0)

        ''ConnectIndexes(6, 5, 2)
        ''ConnectIndexes(2, 1, 5)

        ''ConnectIndexes(5, 7, 1)
        ''ConnectIndexes(1, 3, 5)

        ''ConnectIndexes(0, 3, 7)
        ''ConnectIndexes(0, 7, 4)
        'FaceCount = vCount / 3






        'Tr = Nothing
        'Tri = Nothing


    End Sub


    'Sub Extrude(depth As Single)
    '    Dim y2 As Single
    '    Dim x2 As Single
    '    Dim y1 As Single
    '    Dim x1 As Single


    '    Dim i2, i0, i, i1, i3, j As Short
    '    Dim h, b, n As Single
    '    Dim Tri As New clsTri
    '    Dim P(2) As Vector2

    '    Dim h0 As Boolean

    '    b = 0
    '    h = depth
    '    h0 = h = 0 ' Depthno passed, assumes to get a simple polygon
    '    FaceCount = 0
    '    For i = 1 To nv
    '        Tri.AddPoint(v2D(i).X, v2D(i).Y)
    '    Next


    '    If Tri.Triangola Then
    '        n = -1
    '        For i = Tri.nTriangles - 1 To 0 Step -1
    '            Tri.GetTriangle(i, i1, i2, i3)
    '            P(0) = GetTexCoord(v2D(i1))
    '            P(1) = GetTexCoord(v2D(i2))
    '            P(2) = GetTexCoord(v2D(i3))
    '            AddVertex(v2D(i1).X, b, v2D(i1).Y, n, P(0).X, P(0).Y)
    '            AddVertex(v2D(i2).X, b, v2D(i2).Y, n, P(1).X, P(1).Y)
    '            AddVertex(v2D(i3).X, b, v2D(i3).Y, n, P(2).X, P(2).Y)
    '            i0 = vCount - 3 : i1 = vCount - 2 : i2 = vCount - 1
    '            ConnectIndexes(i0, i1, i2)
    '            FaceCount += 1
    '        Next
    '        If Not h0 Then
    '            n = 1
    '            For i = Tri.nTriangles - 1 To 0 Step -1
    '                Tri.GetTriangle(i, i1, i2, i3)
    '                P(0) = GetTexCoord(v2D(i1))
    '                P(1) = GetTexCoord(v2D(i2))
    '                P(2) = GetTexCoord(v2D(i3))
    '                AddVertex(v2D(i1).X, h, v2D(i1).Y, n, P(0).X, P(0).Y)
    '                AddVertex(v2D(i2).X, h, v2D(i2).Y, n, P(1).X, P(1).Y)
    '                AddVertex(v2D(i3).X, h, v2D(i3).Y, n, P(2).X, P(2).Y)
    '                i0 = vCount - 3 : i1 = vCount - 2 : i2 = vCount - 1
    '                ConnectIndexes(i0, i1, i2)
    '                FaceCount += 1
    '            Next
    '        End If

    '    End If

    '    If h0 Then Exit Sub

    '    For i = 1 To nv
    '        If i = nv Then j = 1 Else j = i + 1
    '        x1 = v2D(i).X
    '        y1 = v2D(i).Y
    '        x2 = v2D(j).X
    '        y2 = v2D(j).Y

    '        If i Mod 2 Then n = -1 Else n = 1
    '        AddVertex(x1, b, y1, n, 0, 0)
    '        AddVertex(x1, h, y1, n, 1, 0)
    '        AddVertex(x2, h, y2, n, 1, 1)
    '        AddVertex(x2, b, y2, n, 0, 1)

    '        i0 = vCount - 4 : i1 = vCount - 3 : i2 = vCount - 2 : i3 = vCount - 1
    '        ConnectIndexes(i0, i1, i2)
    '        ConnectIndexes(i0, i2, i3)
    '        FaceCount += 2
    '    Next


    'End Sub

  
    Sub Init()
        Erase v2D
        nv = 0
        Erase verts
        vCount = 0
        Erase indices
        iCount = 0
        Erase TCTVerts

        Bx.Max.X = -BG
        Bx.Min.X = BG
        Bx.Max.Y = -BG
        Bx.Min.Y = BG
        Bx.Min.Z = BG
        Bx.Max.Z = -BG
    End Sub

    Sub New(mType As EnumMesh)
        Init()
        MeshType = mType
    End Sub

    Sub New(mtype As EnumMesh, ar() As Vector2)
        Init()
        MeshType = mtype
        For Each v As Vector2 In ar
            AddProfileVertex(v.X, v.Y)
        Next
    End Sub

    Sub New()
        Init()
    End Sub

    Function GetCustomVertex() As TransformedColoredTexturedVertex()
        Return TCTVerts
    End Function

    Function ResolveMesh(primitiveName As String) As Primitiva


        Dim m As Primitiva

        m.Faces = FaceCount
        m.VertBuff = New VertexBuffer(g_dev, vCount * TransformedColoredTexturedVertex.SizeInBytes, Usage.WriteOnly, VertexFormat.None, Pool.Managed)
        m.Stream = m.VertBuff.Lock(0, 0, LockFlags.None)
        m.Stream.WriteRange(Of TransformedColoredTexturedVertex)(TCTVerts)
        m.VertBuff.Unlock()
        m.Verticies = TCTVerts
        m.Name = primitiveName

        ' no need indebuffer for custom primitive
        'm.IndexBuff = New IndexBuffer(g_dev, indices.Length * 4, Usage.None, Pool.Managed, True)
        'm.Stream = m.IndexBuff.Lock(0, 0, LockFlags.None)
        'm.Stream.WriteRange(indices)
        'm.IndexBuff.Unlock()
        'm.Stream.Close()

        
            m.Enabled = True

            Return m



    End Function

    Function ResolveMesh() As Direct3D9.Mesh
        Dim r As Result
        Dim m As Direct3D9.Mesh
        If MeshType = EnumMesh.Mesh Then



            Dim elements() As Direct3D9.VertexElement = TransformedColoredTexturedVertex.VertexDescriptor()
            m = New Direct3D9.Mesh(g_dev, FaceCount, TCTVerts.Length + 1, MeshFlags.Managed, elements)
            Dim data As DataStream = m.VertexBuffer.Lock(0, 0, Direct3D9.LockFlags.None)
            data.WriteRange(Of TransformedColoredTexturedVertex)(TCTVerts)
            m.VertexBuffer.Unlock()
            data.Close()


            data = m.IndexBuffer.Lock(0, 0, Direct3D9.LockFlags.None)
            data.WriteRange(indices)
            m.IndexBuffer.Unlock()
            data.Close()

            m.UpdateSemantics(elements)


            m.GenerateAdjacency(0.5F)
            r = m.OptimizeInPlace(Direct3D9.MeshOptimizeFlags.VertexCache)

            m = m.Clone(g_dev, Direct3D9.MeshFlags.Dynamic, TransformedColoredTexturedVertex.Format)
            '       r = m.ComputeNormals()

            data.Dispose()
            Erase elements

            ' Performs tangent frame computations on a mesh. Tangent, binormal, and optionally normal vectors are generated. 
            ' Singularities are handled as required by grouping edges and splitting vertices.
            '
            ' r = m.ComputeTangentFrame(TangentOptions.None)

            Return m

        ElseIf MeshType = EnumMesh.Teapot Then
            Return Direct3D9.Mesh.CreateTeapot(g_dev)

        End If

        '=======================

        'Dim Mesh As New Direct3D9.Mesh
        'Const D3DFVF_VERTEX As Integer = &H112
        ''    Dim vertexbuffer As Direct3DVertexBuffer8

        '' 36     idx, 28 verts

        ''Create an empty d3dxmesh with room for indicies and vertexes
        'Mesh = g_d3dx.CreateMeshFVF(iCount / 2, vCount, DxVBLibA.CONST_D3DXMESH.D3DXMESH_MANAGED, D3DFVF_VERTEX, g_dev)

        'DxVBLibA.D3DAUX.D3DXMeshVertexBuffer8SetData(Mesh, 0, Len(verts(0)) * vCount, 0, verts(0))
        'DxVBLibA.D3DAUX.D3DXMeshIndexBuffer8SetData(Mesh, 0, Len(indices(0)) * iCount, 0, indices(0))
        '' Mesh.ComputeNormals

        'ResolveMesh = Mesh

    End Function


    Public Function GetVertexDescriptor() As VertexElement()



        Return {New VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), _
                                New VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0), _
                                New VertexElement(0, 24, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0), _
                                New VertexElement(0, 36, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Tangent, 0), _
                                New VertexElement(0, 48, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Binormal, 0), _
                                VertexElement.VertexDeclarationEnd}
    End Function

    Private Function SetVert4(pos As Vector4, tc As Vector2, color As Integer) As TransformedColoredTexturedVertex

        SetVert4.Position = pos
        SetVert4.TextureCoordinates = tc
        SetVert4.Color = color

    End Function

    Private Function SetVert4(x As Single, y As Single, z As Single, w As Single, n As Single, u As Single, color As Integer) As TransformedColoredTexturedVertex

        SetVert4.Position = New Vector4(x, y, z, w)
        SetVert4.TextureCoordinates = New Vector2(n, u)
        SetVert4.Color = color

    End Function

    'Sub CreateBoxWithTextureCoords(width As Single, height As Single, depth As Single)

    '    Dim retd3dxMesh As Mesh
    '    '   Dim vertexbuffer As Direct3DVertexBuffer8
    '    ReDim verts(28) ' As TransformedColoredTexturedVertex
    '    Dim indices(36) As Integer
    '    Dim w As Single, d As Single, h1 As Single, h2 As Single
    '    w = width / 2
    '    h2 = height / 2
    '    h1 = -height / 2
    '    d = depth / 2

    '    'Create an empty d3dxmesh with room for 12 vertices and 12
    '    '    retd3dxMesh = g_d3dx.CreateMeshFVF(4 * 6, 6 * 6, D3DXMESH_MANAGED, D3DFVF_VERTEX, g_dev)
    '    Dim c As Integer = Color.Cyan.ToArgb



    '    'front face

    '    'add vertices
    '    verts(0) = SetVert4(New Vector4(-w, h2, -d, 1), New Vector2(0, 0), c)
    '    verts(1) = SetVert4(New Vector4(w, h2, -d, 1), New Vector2(1, 0), c)
    '    verts(2) = SetVert4(New Vector4(w, h1, -d, 1), New Vector2(1, 1), c)
    '    verts(3) = SetVert4(New Vector4(-w, h1, -d, 1), New Vector2(0, 0), c)


    '    'With verts(0) : .x = -w : .y = h2 : .Z = -d : .nz = 1 : .tu = 0 : .TV = 0 : End With
    '    'With verts(1) : .x = w : .y = h2 : .Z = -d : .nz = 1 : .tu = 1 : .TV = 0 : End With
    '    'With verts(2) : .x = w : .y = h1 : .Z = -d : .nz = 1 : .tu = 1 : .TV = 1 : End With
    '    'With verts(3) : .x = -w : .y = h1 : .Z = -d : .nz = 1 : .tu = 0 : .TV = 1 : End With

    '    'connect verices to make 2 triangles per face
    '    indices(0) = 0 : indices(1) = 1 : indices(2) = 2
    '    indices(3) = 0 : indices(4) = 2 : indices(5) = 3

    '    'back face
    '    verts(4) = SetVert4(-w, h1, d, -1, 0, 1, c)
    '    verts(5) = SetVert4(w, h1, d, -1, 1, 1, c)
    '    verts(6) = SetVert4(w, h2, d, -1, 1, 0, c)
    '    verts(7) = SetVert4(-w, h2, d, -1, 0, 0, c)
    '    'With verts(4) : .x = -w : .y = h1 : .Z = d : .nz = -1 : .tu = 0 : .TV = 1 : End With
    '    'With verts(5) : .x = w : .y = h1 : .Z = d : .nz = -1 : .tu = 1 : .TV = 1 : End With
    '    'With verts(6) : .x = w : .y = h2 : .Z = d : .nz = -1 : .tu = 1 : .TV = 0 : End With
    '    'With verts(7) : .x = -w : .y = h2 : .Z = d : .nz = -1 : .tu = 0 : .TV = 0 : End With
    '    indices(6) = 4 : indices(7) = 5 : indices(8) = 6
    '    indices(9) = 4 : indices(10) = 6 : indices(11) = 7

    '    'right face
    '    verts(8) = SetVert4(w, h1, -d, -1, 0, 0, c)
    '    verts(9) = SetVert4(w, h1, d, -1, 1, 0, c)
    '    verts(10) = SetVert4(w, h2, d, -1, 1, 1, c)
    '    verts(11) = SetVert4(w, h2, -d, -1, 0, 1, c)
    '    'With verts(8) : .x = w : .y = h1 : .Z = -d : .nx = -1 : .tu = 0 : .TV = 0 : End With
    '    'With verts(9) : .x = w : .y = h1 : .Z = d : .nx = -1 : .tu = 1 : .TV = 0 : End With
    '    'With verts(10) : .x = w : .y = h2 : .Z = d : .nx = -1 : .tu = 1 : .TV = 1 : End With
    '    'With verts(11) : .x = w : .y = h2 : .Z = -d : .nx = -1 : .tu = 0 : .TV = 1 : End With
    '    indices(12) = 8 : indices(13) = 9 : indices(14) = 10
    '    indices(15) = 8 : indices(16) = 10 : indices(17) = 11

    '    'left face
    '    verts(8) = SetVert4(w, h1, -d, -1, 0, 0, c)
    '    'With verts(16) : .x = -w : .y = h2 : .Z = -d : .nx = 1 : .tu = 0 : .TV = 1 : End With
    '    'With verts(17) : .x = -w : .y = h2 : .Z = d : .nx = 1 : .tu = 1 : .TV = 1 : End With
    '    'With verts(18) : .x = -w : .y = h1 : .Z = d : .nx = 1 : .tu = 1 : .TV = 0 : End With
    '    'With verts(19) : .x = -w : .y = h1 : .Z = -d : .nx = 1 : .tu = 0 : .TV = 0 : End With
    '    indices(18) = 16 : indices(19) = 17 : indices(20) = 18
    '    indices(21) = 16 : indices(22) = 18 : indices(23) = 19

    '    'top face
    '    With verts(20) : .x = -w : .y = h2 : .Z = -d : .ny = -1 : .tu = 0 : .TV = 0 : End With
    '    With verts(21) : .x = -w : .y = h2 : .Z = d : .ny = -1 : .tu = 1 : .TV = 0 : End With
    '    With verts(22) : .x = w : .y = h2 : .Z = d : .ny = -1 : .tu = 1 : .TV = 1 : End With
    '    With verts(23) : .x = w : .y = h2 : .Z = -d : .ny = -1 : .tu = 0 : .TV = 1 : End With
    '    indices(24) = 20 : indices(25) = 21 : indices(26) = 22
    '    indices(27) = 20 : indices(28) = 22 : indices(29) = 23

    '    'bottom  face
    '    With verts(24) : .x = w : .y = h1 : .Z = -d : .ny = 1 : .tu = 0 : .TV = 1 : End With
    '    With verts(25) : .x = w : .y = h1 : .Z = d : .ny = 1 : .tu = 1 : .TV = 1 : End With
    '    With verts(26) : .x = -w : .y = h1 : .Z = d : .ny = 1 : .tu = 1 : .TV = 0 : End With
    '    With verts(27) : .x = -w : .y = h1 : .Z = -d : .ny = 1 : .tu = 0 : .TV = 0 : End With
    '    indices(30) = 24 : indices(31) = 25 : indices(32) = 26
    '    indices(33) = 24 : indices(34) = 26 : indices(35) = 27


    '    'D3DXMeshVertexBuffer8SetData(retd3dxMesh, 0, Len(verts(0)) * 28, 0, verts(0))
    '    'D3DXMeshIndexBuffer8SetData(retd3dxMesh, 0, Len(indices(0)) * 36, 0, indices(0))



    '    CreateBoxWithTextureCoords = retd3dxMesh
    'End Sub

End Class